package com.circle.model.module;

import com.circle.model.util.OkHttpFactory;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OkGet {
    private String url;
    private Map<String, String> headers;
    private Map<String, String> params;
    private Map<String, String> cookies;
    private OkHttpClient client;
    private Request.Builder requestBuilder;

    public OkGet(String url) {
        this.url = url;
        this.headers = new HashMap<>();
        this.params = new HashMap<>();
        this.cookies = new HashMap<>();
        this.client = OkHttpFactory.getClient();
        this.requestBuilder = new Request.Builder();
    }

    /**
     * 添加单个请求头
     */
    public OkGet addHeader(String key, String value) {
        this.headers.put(key, value);
        return this;
    }

    /**
     * 添加多个请求头
     */
    public OkGet addHeaders(Map<String, String> headers) {
        if (headers != null) {
            this.headers.putAll(headers);
        }
        return this;
    }

    /**
     * 添加单个查询参数
     */
    public OkGet addParam(String key, String value) {
        this.params.put(key, value);
        return this;
    }

    /**
     * 添加多个查询参数
     */
    public OkGet addParams(Map<String, String> params) {
        if (params != null) {
            this.params.putAll(params);
        }
        return this;
    }

    /**
     * 添加单个Cookie
     */
    public OkGet addCookie(String name, String value) {
        this.cookies.put(name, value);
        return this;
    }

    /**
     * 添加多个Cookie
     */
    public OkGet addCookies(Map<String, String> cookies) {
        if (cookies != null) {
            this.cookies.putAll(cookies);
        }
        return this;
    }

    /**
     * 构建完整的URL（包含查询参数）
     */
    private HttpUrl buildUrl() {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
        }
        return urlBuilder.build();
    }

    /**
     * 构建请求对象
     */
    private Request buildRequest() {
        // 添加URL和查询参数
        requestBuilder.url(buildUrl());

        // 添加请求头
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            requestBuilder.addHeader(entry.getKey(), entry.getValue());
        }

        // 处理Cookie
        if (!cookies.isEmpty()) {
            StringBuilder cookieString = new StringBuilder();
            for (Map.Entry<String, String> cookie : cookies.entrySet()) {
                if (cookieString.length() > 0) {
                    cookieString.append("; ");
                }
                cookieString.append(cookie.getKey()).append("=").append(cookie.getValue());
            }
            requestBuilder.addHeader("Cookie", cookieString.toString());
        }

        // 设置GET方法
        requestBuilder.get();

        return requestBuilder.build();
    }

    /**
     * 同步执行GET请求
     */
    public String execute() throws IOException {
        Request request = buildRequest();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response);
            }
            return response.body() != null ? response.body().string() : "";
        }
    }

    /**
     * 异步执行GET请求
     */
    public void enqueue(Callback callback) {
        Request request = buildRequest();
        client.newCall(request).enqueue(callback);
    }
}
